<?php
/*
 * @name 文章逻辑处理处理类
 * @author Jin
 * @date 2017.12.28
 **/
namespace app\admin\controller;

use app\common\model\Article as ArticleCommonModel;
use app\common\model\ArticleCat;
use think\Db;
use think\facade\Request;

class Article extends Base
{
    /*
     * @name 展示文章
     * @return articlelist.html
     **/
    public function articleList($value = '')
    {
        $where = [];
        if (!empty(Request::param('art_cat_id'))) {
            $where[] = ['art_cat_id', '=', Request::param('art_cat_id')];
        }
        if (!empty(Request::param('art_title'))) {
            $where[] = ['art_title', 'like', '%' . Request::param('art_title') . '%'];
        }
        $articlelist = ArticleCommonModel::where($where)->order('art_addtime', 'desc')->paginate(10, false, [
            'type' => 'bootstrap',
            'path' => url('Article/articleList', ['art_cat_id' => Request::param('art_cat_id'),'art_title' => Request::param('art_title')]),
        ]);
        $articlecount = ArticleCommonModel::count();
        foreach ($articlelist as $k => $v) {
            $articlelist[$k]['cat_name'] = ArticleCat::where('cat_id', $v['art_cat_id'])->value('cat_name');
        }
        $articlecat = ArticleCat::select();
        $this->assign('articlecat', $articlecat);
        $this->assign('articlecount', $articlecount);
        $this->assign('articlelist', $articlelist);
        return $this->fetch('articlelist');
    }

    /*
     * @name 添加和修改文章
     * @return articleadd.html
     **/
    public function articleAddEdit()
    {
        //显示添加或修改
        if (empty(Request::post())) {
            if (empty(Request::param('art_id'))) {
                $articleid = [
                    "art_id" => "",
                    "art_cat_id" => "",
                    "art_title" => "",
                    "art_content" => "",
                    "art_author" => "",
                    "art_addtime" => "",
                    "art_desc" => "",
                    "art_click" => "",
                    "art_img" => "",
                ];
            } else {
                //接受id
                $art_id = Request::param('art_id');
                //查询该id数据发送到前台
                $articleid = ArticleCommonModel::where('art_id', $art_id)->find();
            }
            //将单条数据发送到前台
            $this->assign('articleid', $articleid);
            //将分类发送到前台
            $articlecat = ArticleCat::select();
            $this->assign('articlecat', $articlecat);
            return $this->fetch('articleaddedit');
        } else {
            //接受上传的所有表单值
            $data = Request::post();
            //接受上传的文件
            $file = request()->file('art_img');
            //判断是否上传图片
            if ($file != '') {
                // 移动到框架应用目录下,限制类型大小
                $fileinfo = $file->validate(['size' => 2097152, 'ext' => 'jpg,png,jpeg'])->move('public/upload/article');
                if ($fileinfo) {
                    $data['art_img'] = $fileinfo->getPathName();
                    //将原文件删除
                    if (is_file($data['old_art_img'])) {
                        unlink($data['old_art_img']);
                    }
                } else {
                    $message = ['info' => 3, 'msg' => $file->getError()];
                    return $message;
                }
            }
            unset($data['old_art_img']);
            if ($data['art_id'] == '') {
                //执行添加
                $data['art_addtime'] = time();
                $res = ArticleCommonModel::insert($data);
                if ($res) {
                    $message = ['info' => 2, 'msg' => '文章添加成功!'];
                    return $message;
                } else {
                    if ($file != '') {
                        unlink($fileinfo->getPathName());
                    }
                    $message = ['info' => 4, 'msg' => '服务器错误,添加失败,请稍后重试!'];
                    return $message;
                }
            } else {
                //执行修改
                $res = ArticleCommonModel::where('art_id', $data['art_id'])->update($data);
                if ($res) {
                    $message = ['info' => 2, 'msg' => '文章修改成功!'];
                    return $message;
                } else {
                    if ($file != '') {
                        unlink($fileinfo->getPathName());
                    }
                    $message = ['info' => 4, 'msg' => '服务器错误,修改失败,请稍后重试!'];
                    return $message;
                }
            }
        }
    }

    /*
     * @name 文章删除
     * @return 状态1成功2失败
     **/
    public function articleDel($value = '')
    {
        if (!empty(Request::post('art_id'))) {
            $filename = ArticleCommonModel::where('art_id', Request::post('art_id'))->value('art_img');
            $catlist = ArticleCommonModel::where('art_id', Request::post('art_id'))->delete();
            if ($catlist) {
                if (is_file($filename)) {
                    unlink($filename);
                }
                return 1;
            } else {
                return 2;
            }
        } else {
            $this->error('服务器错误,请稍后重试!');
        }
    }

    /*
     * @name 文章分类
     * @return catlist.html
     **/
    public function catList($value = '')
    {
        $where = [];
        $catlist = ArticleCat::where($where)->select();
        $this->assign('catlist', $catlist);
        return $this->fetch('catlist');
    }
    /**
     * @name 文章分类添加
     * @return catadd.html
     **/
    public function catAdd()
    {
        if ($this->request->method() === "POST") {
            $acle = new ArticleCat;
            $res = $acle->data([
                'cat_name' => $_POST['cat_name'],
                'cat_desc' => $_POST['cat_desc'],
                'cat_parentid' => 0,
            ])->save();
            if ($res) {
                return array('status' => '1', 'msg' => '添加成功');
            } else {
                return array('status' => '2', 'msg' => '添加失败');
            }
        }
        return $this->fetch();
    }
    /**
     * @name 文章分类修改
     * @return catedit.html
     **/
    public function catEdit()
    {
        $acle = new ArticleCat;
        if ($this->request->method() === "POST") {
            $id = $_POST['cat_id'];
            $row = $acle->allowField(['cat_name', 'cat_desc'])->save($_POST, ['cat_id' => $id]);
            if ($row) {
                return array('status' => '1', 'msg' => '更新成功');
            } else {
                return array('status' => '2', 'msg' => '更新失败');
            }
        } else {
            $res = $acle::get($this->request->param('id'));
            $this->assign('res', $res);
            return $this->fetch();
        }
    }
    /**
     * @name 文章分类删除
     * @return 趙
     **/
    public function delFun()
    {
        if ($this->request->method() === "POST") {
            $acle = new ArticleCat;
            //$res = ArticleCommonModel::with('cat')->where('cat_id',Request::post('cat_id'));
            $res = Db::table('rbw_article_cat')
                ->where('cat_id', Request::post('cat_id'))
                ->alias('a')
                ->join(['rbw_article' => 'b'], 'a.cat_id=b.art_cat_id')->select();
            if ($res) {
                return array('status' => '2', 'msg' => '删除失败,此分类下有文章');
            } else {
                $del = $acle::destroy(Request::post('cat_id'));
                if ($del == 1) {
                    return array('status' => '1', 'msg' => '删除成功');
                } else {
                    return array('status' => '2', 'msg' => '删除失败,未知');
                }
            }
        }
    }
}
